<?php
namespace app\common\model;

use app\extra\BaseModel;
use app\extra\ApiToken;
use app\extra\DBCont;

class Business extends BaseModel
{
    use \think\model\concern\SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $auto = ['user_id','auth','media'];
    public $listAllowField = [];
    public $listExceptField = ['content','media'];
    public function setUserIdAttr(){
        return getUserID();
    }
    public function setAuthAttr($value){
        if (!empty($value)){
            return $value;
        }
        $name = getUserName();
        return  empty($name)? '匿名作者' : $name;
    }
    // 图片
    public function setListStyleAttr($value){
        return serializeByArray($value);
    }
    // 首页展示
    public function setHomeAttr($value){
        return serializeByArray($value);
    }
    public function getListStyleAttr($value){
        return unSerializeByJson($value);
    }

    public function getHomeAttr($value){
        return unSerializeByJson($value);
        /*$value = unSerializeByJson($value);
        if (array_key_exists('img', $value) && !empty($value['img'])){
            $value['img'] = list_added_domain($value['img']);
        }
        return $value;*/
    }
    // 标签
    public function setTagsAttr($value){
        return serializeByArray($value);
    }
    public function getTagsAttr($value){
        return unSerializeByJson($value);
    }
    // 附件
    public function setMediaAttr($value){
        return serializeByArray($value);
    }
    public function getMediaAttr($value){
        return unSerializeByJson($value);
    }
    // 封面
    /*public function getCoverLinkAttr($value){
        return added_domain($value);
    }*/
    /**
     * 添加额外参数
     * @param $item
     */
    public function setAddedField(&$item){
        $item['cate_desc'] = $this->getAddedFieldValue('BusinessCate', 'id', $item['cate_id'], 'title');
        $item['user_desc'] = model('Admin')->getUserName($item['user_id']);
        // $item['public_desc'] = \app\extra\DBCont::getPublishStatus($item['publish']);
    }
    // 更新时间
    public function setChangeTimeAttr($value){
        return strtotime($value);
    }
    public function getChangeTimeAttr($value){
        return showTime($value);
    }
    // 附件
    public function setAttachmentAttr($value){
        return serializeByArray($value);
    }
    public function getAttachmentAttr($value){
        return unSerializeByJson($value);
    }
}